package managerview;

import View.View;
import check.CheckBoxEditor;
import check.CheckBoxRenderer;
import customerview.ShoppingCart;
import users.ManagerFun;
import utils.Utils;
import View.View;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class EditBook extends JFrame {
    String username;
    public EditBook(String username){
        this.username = username;
        //初始化界面
        initJFrame();
        //在这个界面添加内容
        initView();
        //让页面展示出来
        this.setVisible(true);

    }

    private void initView() {

        //添加搜索输入框
        JTextField searchText = new JTextField("（请输入图书编号）", 10);
        searchText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                if (searchText.getText().equals("（请输入图书编号）")) {
                    searchText.setText(""); // 清空文本框供用户输入
                }
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (searchText.getText().isEmpty()) {
                    searchText.setText("（请输入图书编号）"); // 用户未输入内容时恢复提示信息
                }
            }
        });
        searchText.setBounds(30,10,130,30);
        this.getContentPane().add(searchText);

        //添加查询按钮
        JButton searchButton=new JButton();
        searchButton.setBounds(170,10,80,30);
        searchButton.setText("查询");
        this.getContentPane().add(searchButton);
        //添加监听
        //.....
        
        //添加图书按钮
        JButton addButton = new JButton();
        addButton.setText("添加图书");
        addButton.setBounds(350, 9, 87, 30);
        getContentPane().add(addButton);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddBook();
            }
        });

        //添加返回主界面按钮
        JButton ReturnButton=new JButton();
        ReturnButton.setBounds(530,10,130,30);
        ReturnButton.setText("返回主界面");
        this.getContentPane().add(ReturnButton);
        //添加监听
        ReturnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                EditBook.this.setVisible(false);
                new View(username);
            }
        });
        // 初始化表格
        DefaultTableModel model = new DefaultTableModel(0, 4);

        // 如果需要可以给表格添加列名
        model.setColumnIdentifiers(new String[]{"选择","商品", "价格", "库存数量"});


        // 创建一个JTable实例并应用模型
        JTable table = new JTable(model);

        // 可选：使用自定义渲染器或编辑器，例如复选框
        table.getColumnModel().getColumn(0).setCellRenderer(new CheckBoxRenderer());
        table.getColumnModel().getColumn(0).setCellEditor(new CheckBoxEditor((new JCheckBox())));

        //多行复选框只能选一个，想使用就取消注释
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = table.rowAtPoint(e.getPoint());
                for (int i = 0; i < model.getRowCount(); i++) {
                    model.setValueAt(false, i, 0); // 先取消所有行的选中
                }
                model.setValueAt(true, row, 0); // 选中当前行

            }

        });

//        获取所以图书信息并渲染到表格
        try{
            //获取所有图书信息
            Connection connection= Utils.getConnection();
            //定义sql语句
            String sql="select * from book";
            Statement stmt = connection.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                String name = rs.getString("name");
                Double price = rs.getDouble("price");
                int num = rs.getInt("num");
                model.addRow(new Object[]{Boolean.FALSE,name,price,num});
            }
            Utils.Close(stmt,connection);
        }catch (Exception e){

        }

        // 使用JScrollPane包装表格以自动处理滚动条
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(20, 50, 660, 350); // 设置滚动面板的位置和大小
        this.getContentPane().add(scrollPane); // 将滚动面板添加到窗体的内容窗格中

        // 添加监听代码...


        //添加直接购买按钮
        JButton deletebutton=new JButton();
        deletebutton.setBounds(540,410,130,30);
        deletebutton.setText("直接删除");
        this.getContentPane().add(deletebutton);
        //添加监听
        //.....
        deletebutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ManagerFun.deleteall(table);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }

    private void initJFrame() {
        this.setTitle("管理图书");
        this.setSize(700, 500);
        //设置页面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //取消按钮等元素居中放置
        this.setLayout(null);
    }
}
